//
//  ContentViewController.m
//  TipsProject
//
//  Created by Suny on 15/5/14.
//  Copyright (c) 2015年 Suny. All rights reserved.
//

#import "ContentViewController.h"
#import "RefreshControl.h"
#define NUMBER_OF_COLUMNS 2
#import "TipsService.h"
#import "TipAbstract.h"

@interface ContentViewController ()
@property(nonatomic) NSInteger currentPage;
@property(nonatomic, strong) NSMutableArray *imageUrls;
@property(nonatomic, strong) RefreshControl *refreshControl;
@property (nonatomic, strong)TipsService *tipService;
@end

@implementation ContentViewController

- (TipsService *)tipService
{
	if (!_tipService)
	{
		_tipService = [[TipsService alloc] initDefaultBaseUrlString];
	}
	return _tipService;
}

- (WaterFlowView *)waterFlowView
{
    if (!_waterFlowView)
    {
        WaterFlowView * flowView = [[WaterFlowView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 140)];
        flowView.flowdatasource = self;
        flowView.flowdelegate = self;
		_waterFlowView = flowView;
    }
    return _waterFlowView ;
}

- (RefreshControl *)refreshControl
{
	if (!_refreshControl)
	{
		_refreshControl = [RefreshControl refreshControlWithAttachedView:self.waterFlowView
																   style:RefreshControlStyleArrow];

	}
	return _refreshControl;
	
}



- (NSMutableArray *)imageUrls{
	if (!_imageUrls)
	{
		_imageUrls = [NSMutableArray array];
	}
	
	return _imageUrls;
}



- (void)viewDidLoad
{
    [super viewDidLoad];


    [self.view addSubview:self.waterFlowView];
	
	[self.refreshControl addTarget:self 
						action:@selector(triggerRefresh) 
			  forControlEvents:UIControlEventValueChanged];

    self.currentPage = 0;
	
	

}

- (void)viewWillAppear:(BOOL)animated
{
	if (self.isFirstAppear)
	{
		[self.refreshControl beginRefreshing];
	}
}

- (void)triggerRefresh
{
	self.currentPage = 0;
	WEAKSELF
	[self.tipService getTipsFromServe:self.slideTabInfo.tabId 
					   withPageNumber:self.currentPage 
						  finishBlock:^(BaseResponse *response) {
							  DLogObject(response);
							  STRONGSELF
							  [strongSelf handleGetTipResponse:response ];
							  
						  }];
}

- (void)handleGetTipResponse:(BaseResponse *)response
{
	if (response.success)
	{
		[self.imageUrls addObjectsFromArray:response.object];
		
		[self.waterFlowView reloadData];
	}
	else
	{
		[self showErrorMessage:response.desc];
	}
	
	[self.refreshControl endRefreshing];
	
}


- (void)dealloc
{
	[self.refreshControl removeObserver];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (void)viewDidAppear:(BOOL)animated
{
    [_waterFlowView reloadData];  //safer to do it here, in case it may delay viewDidLoad
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark-
#pragma mark- WaterflowDataSource

- (NSInteger)numberOfColumnsInFlowView:(WaterFlowView *)flowView
{
    return NUMBER_OF_COLUMNS;
}

- (NSInteger)flowView:(WaterFlowView *)flowView numberOfRowsInColumn:(NSInteger)column
{
    return 10;
}

- (WaterFlowCell *)flowView:(WaterFlowView *)flowView_ cellForRowAtIndex:(NSInteger)index
{
    static NSString *CellIdentifier = @"Cell";
    WaterFlowCell *cell = [flowView_ dequeueReusableCellWithIdentifier:CellIdentifier];

    if(cell == nil)
    {
        cell  = [[WaterFlowCell alloc] initWithReuseIdentifier:CellIdentifier];

        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        [cell addSubview:imageView];
        imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.layer.borderColor = [[UIColor whiteColor] CGColor];
        imageView.layer.borderWidth = 1;
        imageView.tag = 1001;
		
		
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, cell.width, 30)];
		label.backgroundColor = [UIColor clearColor];
		label.tag = 1008;
		[cell addSubview:label];
    }

    float height = [self flowView:nil heightForCellAtIndex:index];
	UIImageView *imageView  = (UIImageView *)[cell viewWithTag:1001];

	dispatch_async(dispatch_get_main_queue(), ^{
		imageView.frame = CGRectMake(0, 0, self.view.frame.size.width / NUMBER_OF_COLUMNS, height);

        TipAbstract *abstract = [self.imageUrls objectAtIndex:index];
		
		[imageView setQNImageWithURL:[NSURL URLWithString:abstract.thumbnail]
					 placeholderImage:[UIImage imageNamed:@"default_02"]];

		
	});

	UILabel *label = [cell viewWithTag:1008];
	label.text = [NSString stringWithFormat:@"%d",index];
	label.width = imageView.width;
    return cell;

}

#pragma mark-
#pragma mark- WaterflowDelegate

- (CGFloat)flowView:(WaterFlowView *)flowView heightForCellAtIndex:(NSInteger)index
{
    float height = 0;
    switch (index  % 5) 
	{
        case 0:
            height = 127;
            break;
        case 1:
            height = 100;
            break;
        case 2:
            height = 87;
            break;
        case 3:
            height = 114;
            break;
        case 4:
            height = 140;
            break;
        case 5:
            height = 158;
            break;
        default:
            break;
    }

    return height;
}

- (void)flowView:(WaterFlowView *)flowView didSelectAtCell:(WaterFlowCell *)cell ForIndex:(int)index
{

}

- (void)flowView:(WaterFlowView *)_flowView willLoadData:(int)page
{
    [_flowView reloadData];
}

@end
